package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.domain.DeviceRestart;
import com.ruoyi.domain.Vo.AdvIncomeVo;
import com.ruoyi.domain.Vo.InComeDetailResponse;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvIncome;
import com.ruoyi.service.IAdvIncomeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/income")
public class AdvIncomeController extends BaseController
{
    @Autowired
    private IAdvIncomeService advIncomeService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:income:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvIncome advIncome)
    {
        startPage();
        List<AdvIncome> list = advIncomeService.selectAdvIncomeList(advIncome);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */

    @PreAuthorize("@ss.hasPermi('system:income:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvIncome advIncome)
    {
        List<AdvIncome> list = advIncomeService.selectAdvIncomeList(advIncome);
        ExcelUtil<AdvIncome> util = new ExcelUtil<AdvIncome>(AdvIncome.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */

    @PreAuthorize("@ss.hasPermi('system:income:query')")
    @GetMapping(value = "/{advIncomeId}")
    public AjaxResult getInfo(@PathVariable("advIncomeId") Long advIncomeId)
    {
        return success(advIncomeService.selectAdvIncomeByAdvIncomeId(advIncomeId));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:income:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvIncome advIncome)
    {
        return toAjax(advIncomeService.insertAdvIncome(advIncome));
    }

    /**
     * 修改
     */

    @PreAuthorize("@ss.hasPermi('system:income:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvIncome advIncome)
    {
        return toAjax(advIncomeService.updateAdvIncome(advIncome));
    }

    /**
     * 删除
     */

    @PreAuthorize("@ss.hasPermi('system:income:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advIncomeIds}")
    public AjaxResult remove(@PathVariable Long[] advIncomeIds)
    {
        return toAjax(advIncomeService.deleteAdvIncomeByAdvIncomeIds(advIncomeIds));
    }

    /**
     * 结算
     */

    @Log(title = "", businessType = BusinessType.DELETE)
    @PostMapping("/settleaccount")
    public SimpleResult settleAccount(@RequestBody AdvIncomeVo advIncomeVo)
    {
        boolean isSuccess = advIncomeService.settleAccount(advIncomeVo);
        if (!isSuccess) {
            return SimpleResult.error("结算日期内没有可结算订单");
        }
        return SimpleResult.success("结算完成");
    }

    /**
     * 获取收益settleaccount
     * @param advMembersId
     * @return
     */

    @GetMapping("/getincome/{advMembersId}")
    public SimpleResult getIncome(@PathVariable("advMembersId") Long advMembersId) {
        String amount = advIncomeService.getIncome(advMembersId);
        if(amount == null) {
            return SimpleResult.success(0, "暂无收益");
        }
        return SimpleResult.success(amount, "获取收益完成");
    }

    /**
     * 获取收益详情
     * @param advMembersId
     * @return
     */

    @GetMapping("/getincomedetail/{advMembersId}")
    public SimpleResult getIncomeDetail(@PathVariable("advMembersId") Long advMembersId) {
        List<InComeDetailResponse> incomeDetail = advIncomeService.getIncomeDetail(advMembersId);
        if(incomeDetail == null) {
            return SimpleResult.success("暂无收益");
        }
        return SimpleResult.success(incomeDetail, "获取收益完成");
    }

    /**
     * 批量审批
     */

    @PostMapping("/batchSelect")
    public AjaxResult batchSelect(@RequestBody DeviceRestart deviceRestart) {
        System.out.println("我的数据--->"+deviceRestart);
        int result = advIncomeService.batchSelect(deviceRestart.getIds(),deviceRestart.getReboottime());
        return AjaxResult.success("批量修改成功");
    }

}
